<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="通道列表" width="800px" @ok="handleSubmit">
    <div style="font-size:18px;color:#000;padding-left: 20px;">当前规则：{{ruleName}}</div>
    <!-- <BasicTable @register="registerTable" >
      <template #tableTitle>
        <a-button type="primary" @click="handleCreate" preIcon="ant-design:plus-outlined"> 选择通道</a-button>
      </template>
      <template v-slot:bodyCell="{column, record, index}">
        <template v-if="column.dataIndex ==='action'">
          <TableAction :actions="getTableAction(record)" />
        </template>
      </template>
    </BasicTable> -->
    <div style="padding-left: 20px" >
       <a-transfer 
                v-model:target-keys="targetKeys"
                v-model:selected-keys="selectedKeys"
                :data-source="allChannelData"
                :titles="['可选通道', '已选通道']"
                :render="item => item.name"
            />
    </div>
     
  </BasicDrawer>
  <ChannelSelectModal @register="registerModal" @select="selectOk" />
</template>
<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/src/components/Drawer';
  import { BasicTable, useTable, TableAction } from '/src/components/Table';
  import { useModal } from '/src/components/Modal';
  import { useDesign } from '/@/hooks/web/useDesign';
  // import DevSelectModal from './DevSelectModal.vue';
  import ChannelSelectModal from './ChannelSelectModal.vue';
  import { channelColumns, channelSearchFormSchema } from '../AcRule.data';
  import { ruleChannelList,addRuleChannel } from '../AcRule.api';
  import { ColEx } from '/@/components/Form/src/types';

  const { prefixCls } = useDesign('row-invalid');
  const ruleId = ref('');
  const ruleName = ref('');
   let targetKeys = ref<string[]>([]);
    let selectedKeys = ref<string[]>([]);
    let allChannelData = ref([])
  //添加通道model
   const [registerModal, { openModal }] = useModal(); 
  const [registerDrawer, { setDrawerProps,closeDrawer }] = useDrawerInner(async (data) => {
    ruleId.value = data.id;
    ruleName.value = data.name;
    ruleChannelList({
      ruleId: ruleId.value
    }).then(res=>{
      res.ssChannelList.forEach(item=>{
        item.key = item.id
      })
      allChannelData.value = res.ssChannelList
      targetKeys.value = []
      res.ruleChannelList.forEach(item=>{
        item.key = item.channelId
        targetKeys.value.push(item.key)
      })
    })
    setDrawerProps({ confirmLoading: false, showFooter: true });


    // setProps({ searchInfo: { ruleId: unref(ruleId) } });
    reload();
  });
  // 自适应列配置
  const adaptiveColProps: Partial<ColEx> = {
    xs: 24, // <576px
    sm: 24, // ≥576px
    md: 24, // ≥768px
    lg: 12, // ≥992px
    xl: 12, // ≥1200px
    xxl: 8, // ≥1600px
  };
  const [registerTable, { reload, setProps }] = useTable({
    //需要配置rowKey，否则会有警告
    rowKey:'ruleId',
    api: ruleChannelList,
    columns: channelColumns,
    formConfig: {
      labelWidth: 65,
      baseColProps: adaptiveColProps,
      labelAlign: 'right',
      labelCol: {
        offset: 1,
        xs: 24,
        sm: 24,
        md: 24,
        lg: 9,
        xl: 7,
        xxl: 4,
      },
      wrapperCol: {},
      schemas: channelSearchFormSchema,
      autoSubmitOnEnter: true,
      actionColOptions: {
        span: 15
      }
    },
    striped: true,
    useSearchForm: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    immediate: false,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
      //slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  /**
   * 新增
   */
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  

  /**
   * 删除
   */
  async function handleDelete(record) {
    // await deleteRuleChannel({ id: record.id }, reload);
  }


 /**
   * 选择已有通道
   */
  function handleSelect() {
    openModal(true);
  }
  /**
   * 添加已有通道
   */
  async function selectOk(val) {
    await addRuleChannel({ ruleId: ruleId.value, channelList: val }, reload);
  }

  async function handleSubmit(){
    let val = targetKeys.value
    await addRuleChannel({ ruleId: ruleId.value, channelList: val }, reload).then(res=>{
      closeDrawer();
    })
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
 
</script>
<style scoped lang="less">
  @prefix-cls: ~'@{namespace}-row-invalid';

  :deep(.@{prefix-cls}) {
    background: #f4f4f4;
    color: #bababa;
  }

  :deep .ant-transfer .ant-transfer-list {
    width: 350px !important;
    height: 700px !important;
  }
</style>
